package com.hxzy.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hxzy.common.annon.Log;
import com.hxzy.common.annon.RepeatSubmit;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.enums.BusinessType;
import com.hxzy.common.enums.ProjectModule;
import com.hxzy.common.vo.R;
import com.hxzy.controller.BaseController;
import com.hxzy.dto.PageDTO;
import com.hxzy.entity.MallCarousel;
import com.hxzy.exception.ServiceException;
import com.hxzy.service.MallCarouselService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * @Author 打工仔小刘
 * @Date 2022/10/25 19:47
 * @PackageName:com.hxzy.controller
 * @ClassName: CarouselController
 * @Description: TODO
 * @Version 1.0
 */
@Api(tags = "轮播图管理")
@RestController
@RequestMapping(value ="/api/carousel")
public class CarouselController extends BaseController{

    @Autowired
    private MallCarouselService carouselService;

    /**
     * 获取数据
     *
     * @param pageDTO 页面dto
     * @return {@link R}
     */
    @ApiOperation(value = "分页查找数据")
    @PostMapping(value = "getData")
    public R getData(@RequestBody PageDTO pageDTO){
        IPage<MallCarousel> page = this.carouselService.getData(pageDTO);
        return super.ipageToPage(page);
    }

    @ApiOperation(value = "根据主键查询")
    @GetMapping(value = "/{carouselId}")
    public R findById(@PathVariable(value = "carouselId") BigDecimal carouselId){
        MallCarousel mallCarousel = this.carouselService.getById(carouselId);
        mallCarousel.setUpdateBy(null);
        mallCarousel.setUpdateTime(null);
        mallCarousel.setCreateBy(null);
        mallCarousel.setCreateTime(null);
        if(mallCarousel==null){
            return R.build(AckCode.NOT_FOUND_DATA);
        }
        return R.okHasData(mallCarousel);
    }

    @Log(title = ProjectModule.Carousel,businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增轮播图")
    @PostMapping(value = "/addCarousel")
    public R addCarousel(@RequestBody MallCarousel carousel){
        boolean save = this.carouselService.save(carousel);
        if (!save){
            throw new ServiceException(AckCode.COMMON_FRE_OPERATION);
        }
        return R.okHasData(save);
    }

    @Log(title = ProjectModule.Carousel,businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改轮播图")
    @PostMapping(value = "/editCarousel")
    public R editCarousel(@RequestBody MallCarousel carousel){
        boolean b = this.carouselService.updateById(carousel);
        if (!b) {
            throw new ServiceException(AckCode.COMMON_FRE_OPERATION);
        }
        return R.okHasData(b);
    }

    @ApiOperation(value = "删除轮播图")
    @DeleteMapping
    public R delCarousel(Long[] deleteIds){
       if(deleteIds!=null && deleteIds.length>0) {
           boolean result=this.carouselService.updateIsDeletedByCarouselId(deleteIds);
           return super.toAjax(result);
       }

       return  R.build(AckCode.USER_PARAM_IS_NOT_NULL);
    }


}
